package org.xsk.mall.ums.config;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.xsk.mall.ums.domain.model.UmsAdminModel;
import org.xsk.mall.ums.domain.model.UmsPermissionModel;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * description: SpringSecurity需要的用户详情
 * <p>
 * date: 2021/5/20 11:07
 * author: x-one
 */
public class AdminUserDetails implements UserDetails {

    private UmsAdminModel umsAdmin;
    private List<UmsPermissionModel> permissionList;

    public AdminUserDetails(UmsAdminModel umsAdmin, List<UmsPermissionModel> permissionList) {
        this.umsAdmin = umsAdmin;
        this.permissionList = permissionList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return permissionList.stream()
                .filter(permission -> permission.getValue() != null)
                .map(permission -> new SimpleGrantedAuthority(permission.getValue()))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return umsAdmin.getPassword();
    }

    @Override
    public String getUsername() {
        return umsAdmin.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return umsAdmin.getStatus().equals(1);
    }
}
